perm filename SEC1.TEX[105,CSD] blob sn#536190 filedate 1980-09-18 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00008 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	\sneakhead{To the Student}
C00004 00003	\specialbegin{Programs}
C00006 00004	\sneakhead{Declarations} \sendindex{Declarations}
C00007 00005	\head{Write Commands} \sendindex{Write}
C00014 00006	\specialbegin{Iteration} \sendindex{Iteration}
C00024 00007	\specialbegin{Nested Iteration}\sendindex{Nested Iteration}
C00034 00008	\specialbegin{Compound Statements} \sendindex{compund statements}
C00049 ENDMK
C⊗;
\sneakhead{To the Student}

As these notes are being edited for the Fall quarter of 1979, the Pascal
language and the LOTS DEC-20 computer system on which it runs, are
changing frequently. If things don't seem to work as they
have been described here, check with the Pascal course consultants at LOTS,
who keep in touch both with the classroom lectures and assignments, and
with the changes in the system. I don't recommend asking for help from LOTS staff
members; they are very capable and well-meaning, but are not in touch with
what you do and don't know on  a particular day, and with what we are trying to 
get you to learn.

The purpose of a programming assignment is not primarily to get the right answer.
If we ask you ``Why is this in your program?'' and you answer ``The guy at LOTS said
this would make it work,'' we and you are wasting our time.

\rjustline{Bob Floyd}

\ctrline{\bf ``The purpose of computing is insight, not numbers.''}
\ctrline{\qquad\qquad (Ancient Eastern proverb)}

\specialbegin{Programs}

A {\sl program} \sendindex{program} in Pascal is an order to the computer,
describing how a certain
computation is to be carried out.  This order contains both information about
the resources the computer will need to perform the computation, and detailed
instructions about the sequence of information processing operations needed.
This information, and these instructions, are written in precisely defined
notations, as {\sl declarations} \sendindex{declarations} and
{\sl commands} \sendindex{commands} respectively.  
For the moment, all the programs we write will be of the form

\startcode
        PROGRAM $\Nscr$(OUTPUT);
        $\Dscr↓{1}$;
        $\Dscr↓{2}$;
           .
           .
           .
        $\Dscr↓{m}$;
        BEGIN
        $\Cscr↓{1}$;
        $\Cscr↓{2}$;
           .
           .
           .
        $\Cscr↓{n}$
        END.
\endcode

\noindent 
where $\Nscr$, the name of the program, can be any name you like made 
from the English alphabet, the $\Dscr$'s are declarations, and the $\Cscr$'s are 
commands.
\sneakhead{Declarations} \sendindex{Declarations}

Declarations, for the moment, will tell the translator the names we plan
to use for some variables \sendindex{variables} which will have integer
(whole number) values.  To say
that we plan to use three such variables and call them {\tt J, N}, and  {\tt EUCLID},
we would include in the program the declaration
\wuncode{VAR J, N, EUCLID : INTEGER}

\head{Write Commands} \sendindex{Write}

     The commands will be of many kinds, but every program will include at least
one command to print its results so that we can see them.  (Most of a program's
computation will never be seen by humans.  We will often have it print not only
the final results we want, but some intermediate numbers for checking purposes.)

     The command which orders the computer to print a number, say 1979, is
\wuncode{WRITE(1979)}

If we want to print several numbers, we include them all in the parentheses,
separated by commas.  The command
\wuncode{WRITE(1776, 1979, 1984)}
will make the computer print
\wuncode{1776  1979  1984}

If we don't know the actual number we want printed, but do know an expression
for it, for example as a sum or product of other numbers, we can use that
expression in the printing command:
\wuncode{WRITE(3+5, 22/7, SIN(3.1415927/4))}
orders the computer to print
\wuncode{8  3.142857134  7.071067690E-01}

     If we want messages interspersed with the printed numbers, we can enclose
them in quotation marks \sendindex{quotation marks} {\tt ' '}, otherwise treating them like the numbers in the
printing command:
\wuncode{WRITE('PI IS ABOUT',22/7,'THE U.S. IS',1979-1776,'YEARS OLD')}
orders the computer to print:
\wuncode{PI IS ABOUT 3.142857134 THE U.S. IS 203 YEARS OLD}
(Notice that the computer is saying these things because it has been told to,
not because they are true.  Computers are not interested in truth.)

     If we want to print information on several lines, we can use the 
{\tt WRITELN} command. After a {\tt WRITELN}, subsequent printing will start
on a new line.  A short program using {\tt WRITELN}:

\startcode
     PROGRAM TABLE;
     BEGIN
     WRITELN('SHORT TABLE OF PERFECT SQUARES');
     WRITELN(1*1, 2*2, 3*3, 4*4, 5*5);
     WRITE(6*6, 7*7, 8*8, 9*9, 10*10)
     END.
\endcode

\noindent 
will print this:

\topoutput
     SHORT TABLE OF PERFECT SQUARES
      1   4   9  16   25
     36  49  64  81  100
\botoutput

\noindent 
(We shall use these boxes around the results from a 
program when the results use more than one line.)

(The asterisk, {\tt *}, is used as a multiplication symbol in Pascal.  The
more traditional $\times$ and $\cdot$ are too easily confused with the letter and
the decimal point to be satisfactory in computer languages.)

The occurences of expressions in printing commands are
called {\sl operands} \sendindex{operands} of these commands.

We may use {\tt WRITELN} all by itself as a command to end a line in the output.
The command {\tt WRITELN(A,B)} means the same as 
\startcode
        WRITE(A,B);
        WRITELN
\endcode

The information ``printed'' by the {\tt WRITE} and {\tt WRITELN} commands does
not go directly to the printer.  Rather, it is kept in the Pascal program
in a file called OUTPUT, and eventually sent to a 
destination specified by the user of the program.  When the program
is executed, it starts by asking where you want the output sent.
If you want only to see the output on the terminal screen, reply TTY: 
followed by a carriage return.  (TTY, standing for ``teletype,'' refers to
a terminal, even though few terminals are teletype machines any more.
Notice that you must type the ``:'' following TTY.)
If you want the output to go to a file in your directory called MYPROG.OUT,
type that filename in, followed by a carriage return.
%(DO NOT accidentally type MYPROG.PGO here; you will replace your
%program file with your output.  If you do, find a TA or
%consultant immediately.  DON'T log out!  Somebody may be able to retrieve
%your program file.)
After the program is 
executed, you can inspect the results with the TYPE command, or print
it out on paper with the PRINT command.  If you give Pascal no filename
(by just typing a carriage return), the output will go in your directory in a
file called OUTPUT.
\specialbegin{Iteration} \sendindex{Iteration}

Our last example could as well have been done by a hand calculator as by
a computer, since we had to write a separate formula for each number we wanted
to print.  Since there was a general pattern to all these formulas, it would be
better if we could write a command that would briefly describe this general
pattern.  If, for example, we wanted to write the first 100 square roots on
separate lines, we could use these commands:

\startcode
     WRITELN(SQRT(1));
     WRITELN(SQRT(2));
     WRITELN(SQRT(3));
     .
     .
     .
     WRITELN(SQRT(100))
\endcode

There are two elements in the pattern:

\bitem Every command is of the form
\wuncode{WRITELN(SQRT(I))}
\bitemindent for some number  {\tt I}.

\bitem The series of values that {\tt I} takes on starts with 1, 
always increases by  1, and ends at  100.

     We have completely described the pattern.  We can say the same thing in
Pascal with the program

\startcode
     PROGRAM ITERATE(OUTPUT);
         VAR I : INTEGER;
     BEGIN
         FOR I := 1 TO 100 DO
             WRITELN(SQRT (I) )
     END.
\endcode

Here the second line is a declaration for the variable  {\tt I}, specifying that
the value of {\tt I} can be any integer (whole number).  The fourth line is
an {\sl iterative clause} \sendindex{iterative clause},
causing the immediately following command to be repeated.
It also gives  {\tt I} the values  1, 2, 3, $\ldotss$, 99, 100
for the successive repetitions,
then quits.  Such commands make possible the effective use of the vast speed
of present-day computers, by specifying very concisely a large number of
similar operations.   (We shall later show other types of iterative clause.)
A significant part of the computer programmer's task is to find a way of
viewing a particular problem as a series of similar operations which can be
expressed as an iteration.

Henceforth, we shall often omit the first part (through {\tt BEGIN}) and the last
``{\tt END}'' line of a program, in order to save space.  It must be included when a 
program is executed.

     Generally, if  $\Vscr$  is an integer variable,  $\Escr ↓{1}$ and $\Escr ↓{2}$  
are expressions with numerical values, and  $\Cscr$  is a command, we can write 
an {\sl iterative command} \sendindex{iterative command} of the form:
\wuncode{FOR $\Vscr$ := $\Escr ↓{1}$ TO $\Escr ↓{2}$ DO $\Cscr$}
with the effect that

\bitem $\Vscr$ will be given the value $\Escr ↓{1}$, and $\Cscr$ will be executed.

\bitem $\Vscr$ will be increased by 1, and $\Cscr$ will be executed.

\bitem $\Vscr$ will again be increased by 1, and $\Cscr$ executed, etc.

\bitem As soon as  $\Vscr$  gets larger than  $\Escr ↓{2}$, the process stops
(even if  $\Cscr$  has not
even been executed once); the iterative command is then finished.

\example

\startcode
     FOR A := 1 TO 4 DO
          WRITE(2 * A + 1, 2 * A)
\endcode

\noindent
prints:
\wuncode{3  2  5  4  7  6  9  8}
so does:

\startcode
     FOR A := 0 TO 3 DO
          WRITE(2 * A + 3, 2 * A +2)
\endcode
\sendindex{FOR}
\sendindex{TO}
\sendindex{DO}

\rule{
Indent \sendindex{Indent} the command which is repeated by an iterative clause, 
as in the above examples.
}

\rule{
Use a descriptive name, where possible, for each iteration variable. \sendindex{iteration variable}
Often an iteration enumerates the elements of a set; if so, a natural name
for the iteration variable is the name for an element of the set.
If the iteration is enumerating lines of printed output, the
iteration variable might be {\tt LINE}.  If it is keeping track of
time, call it {\tt TIME} or {\tt CLOCK} or {\tt HOURS}.
}%end rule


\exercise
Write a program which will, for each number from 2 to 100,
print on one line the number, its square, its cube, and its square root.
\endexercise

\exercise
Write a program which will print a temperature conversion table
from degrees Centigrade to degrees Fahrenheit.  The relevant formula is
$F = 9C/5 + 32$, where $C$ is the Centigrade temperature and $F$ is the
Fahrenheit temperature.  The table should go from  -40  degrees to  50  degrees
Centigrade.  ({\bf Warning:}  the formula as given is not in the Pascal language.)
\endexercise

\exercise
Write a program which will, for each number from  10  to 99,
print the number and its logarithm.
\endexercise

\exercise
Write a program which will, for each number in the sequence
0, 0.01, 0.02, $\ldotss$, 1.00, print the number, its sine, and its cosine.
Pascal uses radian measure for angles.
Notice that the iteration variable must be a whole number, changing by 1
at each repetition; you must scale it down to the desired fractional number,
by multiplication or division.
\endexercise

\exercise

Write and execute a Pascal program, using iteration, to print a conversion
table from inches to centimeters in two columns, ranging from one to fifty
inches.  The top and bottom of the table should look like:

\fourcolalign{
INCHES⊗CM⊗INCHES⊗CM\cr
1⊗2.54⊗26⊗66.04\cr
2⊗5.08⊗27⊗68.58\cr
.⊗.⊗.⊗.\cr
.⊗.⊗.⊗.\cr
.⊗.⊗.⊗.\cr
25⊗63.50⊗50⊗127.00\cr
}%end fourcolalign

\endexercise

\newexercise{
Find the right expression  $\Escr$  so that the command:
\wuncode{FOR I := 1 TO 6 DO WRITE($\Escr$)}
will print:
\wuncode{7  11  15  19  23  27}
Find another  $\Escr$  which makes it print:
\wuncode{20  13  6  -1  -8  -15}
Suggest a general rule.  Do the same thing for this command
\wuncode{FOR I := 0 TO 5 DO WRITE($\Escr$)}
Is this easier?
}

\specialbegin{Nested Iteration}\sendindex{Nested Iteration}

The meaning of command (1) below, in which an iterative command is itself
iterated (such a command is called a {\sl nested iteration}),
is interpreted by
first substituting  1  and  2  for {\tt I}, to get the series of commands (2)
and then substituting the appropriate numbers for {\tt J}, to get
the commands (3).  The order of substitution \sendindex{substitution} is important.

Consider the command:

\startcode
FOR I := 1 TO 2 DO                              (1)
   FOR J := I TO 4 DO   
      WRITE(I/J)                
\endcode

First we substitute 1 and 2 for I; the command has the same effect as these two
iterative commands.

\startcode
FOR J := 1 TO 4 DO                              (2)
   WRITE(1/J);
FOR J := 2 TO 4 DO
   WRITE(2/J)
\endcode

\sendnotes{RWF->RWF elaborate on ``The same effect ...''}
The same effect may be obtained, in turn, by these seven printing commands:

\startcode
       WRITE(1/1);                              (3)
       WRITE(1/2);
       WRITE(1/3);
       WRITE(1/4);
       WRITE(2/2);
       WRITE(2/3);
       WRITE(2/4);
\endcode

\noindent
which print:
\wuncode{1.00000  0.500000        0.333333  0.250000  1.00000  0.666667  0.500000}

\exercise
Find the sequence of printing commands equivalent to

\startcode
     FOR K := 1 TO 3 DO
          FOR L := K TO 3 DO
              WRITE(K*L)
\endcode
\endexercise

\example

To write a program to print the multiplication tables for numbers
1  through  10, as shown below:

\topoutput
        1 x 1 = 1
        1 x 2 = 2
        .
        .
        .
        1 x 10 = 10
        2 x 1 = 2
        2 x 2 = 4
        .
        .
        .
        2 x 10 = 20
        3 x 1 = 3
        .
        .
        .
        3 x 10 = 30
        .
        .
        .
        10 x 10 = 100
\botoutput

We could print (say) the third set of ten lines by the command
\wuncode{FOR J := 1 TO 10 DO WRITELN(3,'*',J,'=',3*J),}
and the fourth set by
\wuncode{FOR J := 1 TO 10 DO WRITELN(4,'*',J,'=',4*J)}
Generally, we may print the I-th group of lines by the command
\wuncode{FOR J := 1 TO 10 DO WRITELN(I,'*',J,'=',I*J)}
with the appropriate number substituted for {\tt I}.  On the other hand, the
entire program consists of writing groups 1 though 10; thus the program is

\startcode
     FOR I := 1 TO 10 DO
          Print the I-th set of ten lines
\endcode

Combining these, we get the complete Pascal program:
\startcode
        PROGRAM TABLES;
            VAR I,J : INTEGER;
        BEGIN
            FOR I := 1 TO 10 DO
               FOR J := 1 TO 10 DO
                  WRITELN(I, ' * ',J, ' = ',I*J);
        END.
\endcode
\noindent
which is equivalent to these programs:
(We shall use a vertical line between two programs to mean that they have
the same effect.)

\samecode{
 FOR J := 1 TO 10 DO⊗WRITELN(1,' * ',1,' = ',1);\cr
     WRITELN(1,' * ',J,' = ',1*J);⊗WRITELN(1,' * ',2,' = ',2);\cr
⊗WRITELN(1,' * ',3,' = ',3);\cr
⊗   .\cr
⊗   .\cr
⊗   .\cr
⊗WRITELN(1,' * ',10,' = ',10);\cr
 FOR J := 1 TO 10 DO⊗WRITELN(2,' * ',1,' = ',2);\cr
     WRITELN(2,' * ',J,' = ',2*J);⊗WRITELN(2,' * ',2,' = ',4);\cr
      .⊗     .\cr
      .⊗     .\cr
      .⊗     .\cr
 FOR J := 1 TO 10 DO⊗WRITELN(10,' * ',1,' = ',10);\cr
     WRITELN(10,' * ',J,' = ',10*J);⊗WRITELN(10,' * ',2,' = ',20);\cr
⊗    .\cr
⊗    .\cr
⊗    .\cr
⊗WRITELN(10,' * ',10,' = ',100);\cr
}

\noindent
which prints

\topoutput
     1 * 1 = 1
     1 * 2 = 2
         .
         .
         .
    10 * 10 = 100
\botoutput


\pnote{How Numbers Get Printed}

        If I is an integer-valued expression, printing I by {\tt WRITE(I)}\sendindex{WRITE}
always causes printing \sendindex{printing} of twelve characters, first a number of blanks,
then the decimal digits of the number.  For example, if the value of I
is -123, it is printed as {\tt ≡≡≡≡≡≡≡≡-123}, where each {\tt ≡}
represents a blank.

        If you know that I will not require more than N character positions,
(including the minus sign if I may be negative, and a blank if needed to
separate it from the previous output), you can follow I by a colon and N in the 
{\tt WRITE} command, and I will be printed in an N-character field.  If I
is -123,
\wuncode{WRITE(I:5)}
prints {\tt ≡-123}.  In the previous example, the command
\wuncode{WRITELN(I:3,' * ',J:3,' = ', I*J:4)}
would yield much more attractive, compact, and legible results.

\exercise
   Find a nested iteration to print the numbers
\wuncode{10, 11, 20, 21, 22, 30, 31, 32, 33, 40, 41, 42, 43, 44 }
({\bf Hint:} Break the sequence up into groups, and find an iterative command
for each group.   Look for the common features of all the iterative commands.)
\endexercise

\exercise
Print the integers between  1  and  100  which are {\sl not} perfect
squares, i.e.,
\wuncode{2  3  5  6  7  8  10  11  12  13  14  15  17 $\ldotss$ 99}
(To the teacher:  there are several ways to do this, even using the limited
repertory of operations thus far covered.  Class discussion of their
efficiencies may be rewarding.)
\endexercise
\specialbegin{Compound Statements} \sendindex{compund statements}


Suppose we want the computer to print a large triangle of asterisks, like
this

\topoutput
     *
     **
     ***
     ****
     *****
     ******
\botoutput
\noindent
but fifty lines high.  The pattern is obvious.  Similarly, how to print each
particular line is obvious; for the 43rd, we need the commands

\startcode
     FOR I := 1 TO 43 DO
          WRITE('*');
     WRITELN
\endcode

To avoid writing this out 50 times, changing only the number  43, we see
that it has the general pattern

\startcode
     FOR I := 1 TO L DO
          WRITE('*');
     WRITELN
\endcode
\noindent
and that an iterative clause like
\wuncode{FOR L := 1 TO 50 DO}
would give {\tt L} the right sequence of values.  But if we try putting them
together in a program,

\startcode
     FOR L := 1 TO 50 DO
          FOR I := 1 TO L DO
               WRITE('*');
          WRITELN
\endcode
\noindent
we get, not a triangle, but a number of solid lines of asterisks, totaling
1275.  Why didn't  {\tt WRITELN}       get executed while {\tt L} was  1,
immediately after executing the {\tt I} iteration once and printing one asterisk?
The reason is that an iterative clause only applies to the immediately
following command.  The iterative clause that uses {\tt L} causes repetition of
the next two lines, which are a single complete command.  The last line is a
separate command.  Somehow we need to tie these two commands into one single 
command.

     Suppose  $\Cscr ↓{1}$,$\Cscr ↓{2}$,...,$\Cscr ↓{n}$  are any commands.  To form a single command which
has the effect of executing  $\Cscr ↓{1}$, then  $\Cscr ↓{2}$, etc., through  $\Cscr ↓{n}$, we may write
the {\sl compound statement} \sendindex{compound statement}
\wuncode{BEGIN $\Cscr ↓{1}$; $\Cscr ↓{2}$; $\ldots$ $\Cscr ↓{n}$ END}

The compound statement can then be treated as a whole, for the purposes of 
iteration and for other purposes which we shall encounter.  The words 
{\tt BEGIN} \sendindex{{\tt BEGIN}} and {\tt END} \sendindex{{\tt END}}
serve as parentheses around commands.

\sendnotes{DPB->RWF: I rewrote this rule to eliminate the TEX problem of
	code inside boxes.}
%Indentation
\rule{
To make the extent and structure of compound statements immediate\-ly apparent 
to the eye when reading programs, you should write compound statements so
that each command of the compound statement begins on a new line and the 
entire compound statement is indented further than the line which 
precedes the compound statement.  (Notice that this rule is followed
in the example programs in the text.)
}

\example
\startcode
   FOR $\Vscr$ := $\Eone$ TO $\Etwo$ DO
        BEGIN
        $\Cscr↓{1}$;
        $\Cscr↓{2}$;
        .
        .
        .
        $\Cscr↓{n}$
        END
\endcode

To see the difference that use of compound statement makes,
\wuncode{FOR I := 1 TO 3 DO $\Cscr ↓{1}$; $\Cscr ↓{2}$; $\Cscr ↓{3}$}
has the effect of  $\Cscr ↓{1}$; $\Cscr ↓{1}$; $\Cscr ↓{1}$; $\Cscr ↓{2}$;
$\Cscr ↓{3}$, but
\wuncode{FOR I := 1 TO 3 DO BEGIN $\Cscr ↓{1}$;  $\Cscr ↓{2}$; $\Cscr ↓{3}$ END}
has the effect of  $\Cscr ↓{1}$; $\Cscr ↓{2}$; $\Cscr ↓{3}$;
$\Cscr ↓{1}$; $\Cscr ↓{2}$;
$\Cscr ↓{3}$; $\Cscr ↓{1}$; $\Cscr ↓{2}$; $\Cscr ↓{3}$. Going back to our
triangle problem, we see that we need this program:

\startcode
        PROGRAM TRIANGLE;
            VAR I, L : INTEGER;
        BEGIN
            FOR L := 1 TO 50 DO
                BEGIN
                FOR I := 1 TO L DO
                     WRITE('*');
                WRITELN;
                END
        END.
\endcode

\example

To print this chessboard pattern:

\topoutput
        XXXOOOXXXOOOXXXOOOXXXOOO
        XXXOOOXXXOOOXXXOOOXXXOOO
        XXXOOOXXXOOOXXXOOOXXXOOO
        OOOXXXOOOXXXOOOXXXOOOXXX
        OOOXXXOOOXXXOOOXXXOOOXXX
        OOOXXXOOOXXXOOOXXXOOOXXX
        .  .  .  .  .  .  .  .
        .  .  .  .  .  .  .  .
        .  .  .  .  .  .  .  .
        OOOXXXOOOXXXOOOXXXOOOXXX
\botoutput

The first six lines can be printed by

\startcode
        FOR I := 1 TO 3 DO
             WRITELN('XXXOOOXXXOOOXXXOOOXXXOOO');
        FOR I := 1 TO 3 DO
             WRITELN('OOOXXXOOOXXXOOOXXXOOOXXX')
\endcode

Making a compound statement of this and iterating, we get the program

\startcode
        FOR J := 1 TO 4 DO
             BEGIN
             FOR I := 1 TO 3 DO
                WRITELN('XXXOOOXXXOOOXXXOOOXXXOOO');
             FOR I := 1 TO 3 DO
                WRITELN('OOOXXXOOOXXXOOOXXXOOOXXX')
             END
\endcode

\pnote{Integer Division}\sendindex{Division}

There are two division operators in Pascal.  The form A/B gives as
exact a result as the machine can compute.  The form A DIV B gives the integer
part of the quotient; 7 DIV 3 is 2, for example.  The second form must be
used in contexts, such as the limits of iterations, where integers are
required.

\exercise
Write a program which prints the first 36 numbers in the form:

\topoutput
         0
         1    2
         3    4    5
         6    7    8    9
        10   11   12   13   14
        15   16   17   18   19   20
        21   22   23   24   25   26   27
        28   29   30   31   32   33   34   35
\botoutput
\exercisefont
(Note:  this requires use of the integer division operation, DIV.  See the
Pascal Note above.)

(Clue:  the $i↑{th}$ line starts with a number $i(i-1)/2$.  Try first writing a
program which prints only the first number of each line.)
\endexercise

\exercise
Print, one per line, the two-digit numbers of which the first digit is
larger than the second (10, 20, 21, 30,...,97,98).  This can be done by a nested
iteration, using a single {\tt WRITELN$(\Escr)$}, where $\Escr$ is an
expression.
\endexercise


\exercise
Print the three digit numbers of which the digits are in decreasing order
(210, 310, 320, 321, 410,...986, 987).
\endexercise

\exercise
Write and execute a program to print the design below.  (The border is 4 
characters wide, the inner square is 17 by 17.)  Use iteration as much
as you reasonably can.

\sendnotes{RWF==>Don't know how to do this with TEX. FRG}
\startcode
               *************************
               *************************
               *************************                
               *************************
               *****               *****
               *****               ***** 
               ****  *           *  ****
               ****   *         *   ****
               ****    *       *    ****
               ****     *     *     ****
               ****      *   *      ****
               ****       * *       ****
               ****        *        ****
               ****       * *       ****
               ****      *   *      ****
               ****     *     *     ****
               ****    *       *    ****
               ****   *         *   ****
               ****  *           *  ****
               **** *             * ****
               *****               *****
               *************************
               *************************
               *************************
               *************************
\endcode
\endexercise

\par\vfill\eject        % The next exercise only makes sense on one page.

\exercise
Match the pieces of program below to the pages they print.

\startcode
\sendnotes{FINALCHECK-make sure that all of this exercise is on one page.}
(1)  FOR R := 1 TO 4 DO                         (i)     AB
        FOR C := 1 TO R DO                              AAB
           WRITE('A');                                  AAAB
     WRITELN('B')                                       AAAAB

(2)  FOR R := 1 TO 4 DO                         (ii)    AAAAAAAAAAB
        FOR C := 1 TO R DO
           BEGIN                                (iii)   AB
           WRITE('A');                                  AB
           WRITELN('B')                                 B
           END                                          AB
                                                        B
(3)  FOR R := 1 TO 4 DO                                 B
        BEGIN                                           AB
        FOR C := 1 TO R DO                              B
           WRITE('A');                                  B
        WRITELN('B')                                    B
        END 
                                                (iv)    AB
(4)  FOR R := 1 TO 4 DO                                 AB
        BEGIN                                           AB
        WRITE('A');                                     AB
        FOR C := 1 TO R DO                              AB
           WRITELN('B')                                 AB
        END                                             AB
                                                        AB
(5)  FOR R := 1 TO 4 DO                                 AB
        BEGIN                                           AB
        FOR C := 1 TO R DO
           WRITE('A')
        END;
     WRITELN('B')
\endcode
\endexercise